import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

// 用于包装单个子节点的渲染对象
class CustomPadding extends SingleChildRenderObjectWidget {
  final EdgeInsets padding;
  
  const CustomPadding({
    super.key,
    required this.padding,
    required Widget child,
  }) : super(child: child);
  
  @override
  RenderObject createRenderObject(BuildContext context) {
    return RenderCustomPadding(padding: padding);
  }
  
  @override
  void updateRenderObject(BuildContext context, RenderCustomPadding renderObject) {
    renderObject.padding = padding;
  }
}

// 对应的 RenderObject
class RenderCustomPadding extends RenderProxyBox {
  EdgeInsets _padding;
  
  RenderCustomPadding({
    required EdgeInsets padding,
  }) : _padding = padding;
  
  set padding(EdgeInsets value) {
    if (_padding != value) {
      _padding = value;
      markNeedsLayout();
    }
  }
  
  @override
  void performLayout() {
    if (child != null) {
      // 为子节点应用内边距约束
      child!.layout(
        constraints.deflate(_padding),
        parentUsesSize: true,
      );
      size = Size(
        child!.size.width + _padding.horizontal,
        child!.size.height + _padding.vertical,
      );
    } else {
      size = constraints.smallest;
    }
  }
  
  @override
  void paint(PaintingContext context, Offset offset) {
    if (child != null) {
      // 绘制子节点时应用内边距偏移
      context.paintChild(child!, offset + Offset(_padding.left, _padding.top));
    }
  }
  
  @override
  double computeMinIntrinsicWidth(double height) {
    if (child != null) {
      return child!.getMinIntrinsicWidth(height) + _padding.horizontal;
    }
    return _padding.horizontal;
  }
  
  // 其他 intrinsic 尺寸计算方法...
}